package org.eclipse.wtp.sample.flashcards;

import java.util.*; // for ArrayList

public class FlashCardHand {
	private ArrayList<FlashCard> hand;
	private FlashCardSet set;
	private int cardsInHand;
	private int theOne;
	private Random random;	// for generating random integers

	
	public FlashCardHand(FlashCardSet set) {
		this.set = set;
		this.cardsInHand = 3;
		this.random = new Random();
		this.hand = new ArrayList<FlashCard>();
		this.draw();
	}
	public String getTitle() {return this.set.getTitle();}
	public void draw() {
		// clear hand and get cards from set
		this.hand.clear();
		while (this.hand.size() < 3) {
//		for(int i = 0;i < this.cardsInHand; i++ ) {
			// get a card
			FlashCard temp = this.set.drawCard();
			// determine whether it is already in the hand
			boolean duplicate = false;
			Iterator<FlashCard> itr = this.hand.iterator();
			while (itr.hasNext()){
				FlashCard temp2 = itr.next();
				if (temp.getKey1().equals(temp2.getKey1())) {
					System.out.println("Found a duplicate");
					duplicate = true;
				}
			}
			if (!duplicate) {this.hand.add(temp);}
			//this.hand.add(this.set.drawCard());
		}
		// randomly choose one to be the test word
		this.theOne = random.nextInt(this.hand.size());
	}
	public String getTestKey1() {
		return this.hand.get(theOne).getKey1();
	}
	public String getTestKey2() {
		return this.hand.get(theOne).getKey2();
	}
	public String getCardKey1(int i) {
		// clean input. Forces i into acceptable bounds. 
		if (i < 0) {i = 0;}
		else if (i >= cardsInHand) {i = cardsInHand - 1;}
		return this.hand.get(i).getKey1();
	}
	public String getCardKey2(int i) {
		// clean input. Forces i into acceptable bounds. 
		if (i < 0) {i = 0;}
		else if (i >= cardsInHand) {i = cardsInHand - 1;}
		return this.hand.get(i).getKey2();
	}
	public boolean isCorrect(String attempt) {
		String correct = this.hand.get(this.theOne).getKey2();
		return correct.equals(attempt);
	}

}